package com.stone.back.dish.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.stone.back.dish.entity.BfWeekmenu;
import com.stone.back.dish.entity.BfWeekmenuCombo;
import com.stone.base.dao.BaseDao;
import com.stone.base.entity.Page;

/**
 * 周菜单管理
 * @author Stone P
 *
 */
@Repository
public class WeekMenuDao extends BaseDao implements IWeekMenuDao {
	/**
	 * 查询周菜单分页信息
	 * @param pageNumber	页数
	 * @param startDate		销售日期
	 * @param endDate		销售日期
	 * @return
	 */
	@Override
	public Page selectWeekMenuPage(int pageNumber, String startDate, String endDate) {
		StringBuilder hql = new StringBuilder("from BfWeekmenu where 1=1 ");
		if(StringUtils.isNotBlank(startDate)) {
			hql.append(" and sellweek >= '" + startDate + "'");
		}
		if(StringUtils.isNotBlank(endDate)) {
			hql.append(" and sellweek <= '" + endDate + "'");
		}
		hql.append(" order by sellweek desc, weekmenuid desc");
 		Page page = this.queryPage(hql.toString(), pageNumber);
		return page;
	}

	/**
	 * 增加周菜单
	 * @param weekMenu	周菜单信息
	 * @return
	 */
	@Override
	public Integer addWeekMenu(BfWeekmenu weekMenu) {
		return (Integer) this.hibernateTemplate.save(weekMenu);
	}

	/**
	 * 周菜单和套餐关系
	 * @param weekmenuCombo	周菜单套餐信息
	 * @return
	 */
	@Override
	public Integer addWeekMenuCombo(BfWeekmenuCombo weekmenuCombo) {
		return (Integer) this.hibernateTemplate.save(weekmenuCombo);
	}

	/**
	 * 修改周菜单
	 * @param weekMenu	周菜单信息
	 * @return
	 */
	@Override
	public void updateWeekMenu(BfWeekmenu weekMenu) {
		this.hibernateTemplate.update(weekMenu);
	}

	/**
	 * 删除周菜单和套餐关系
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@Override
	public void deleteWeekMenuDish(int weekMenuId) {
		String sql = "delete from bf_weekmenu_combo where weekmenuid = " + weekMenuId;
		Session session = this.hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	/**
	 * 查询周菜单详情
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@Override
	public BfWeekmenu selectWeekMenuDetail(int weekMenuId) {
		return this.hibernateTemplate.get(BfWeekmenu.class, weekMenuId);
	}

	/**
	 * 查询周菜单和套餐关系详情
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@Override
	public List<BfWeekmenuCombo> selectWeekMenuComboDetail(int weekMenuId) {
		List<BfWeekmenuCombo> comboDishList = this.hibernateTemplate.find("from BfWeekmenuCombo where weekmenuid = ? order by selldate", weekMenuId);
		return comboDishList;
	}

	/**
	 * 删除周菜单
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@Override
	public void deleteWeekMenu(BfWeekmenu weekMenu) {
		this.hibernateTemplate.delete(weekMenu);
	}
}
